# Simple Moving Average (SMA)
import pandas as pd
import statistics as stats

'''
   @ param in
   DataSet: dataset object list 
   TimePeriod: sma cal days
 
   @ return
   DataSet with SimpleMovingAverage data
'''

def cal_sma(DataSet, TimePeriod):
    data_set_ = pd.DataFrame(DataSet)
    data_set_ = data_set_.iloc[::-1]
    close = data_set_['close'].astype(float)
    time_period = TimePeriod
    history = []
    sma_values = []
    for close_price in close:
    	history.append(close_price)
    	if len(history) > time_period:
    		del (history[0])
    	sma_values.append(stats.mean(history))
    data_set_ = data_set_.assign(simple_moving_average=pd.Series(sma_values, index=data_set_.index))
    return data_set_
